package com.koron.tool.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.tool.bean.ToolBean;
import com.koron.tool.bean.ToolGroupBean;
import com.koron.tool.bean.ToolStatisticsBean;
import com.koron.tool.bean.ToolStatisticsQuery;
import com.koron.tool.mapper.ToolAccountStatisticsMapper;
import com.koron.tool.mapper.ToolAccountsManagerMapper;
import com.koron.tool.service.ToolAccountStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: liaodf
 * @Date: 2021/10/25 10:52
 */
@Slf4j
@Service
public class ToolAccountStatisticsServiceImpl implements ToolAccountStatisticsService {

    @Override
    @TaskAnnotation("getStatistics")
    public Response getStatistics(SessionFactory factory, String warehouseId) {
        try {
            if (StringUtils.isEmpty(warehouseId)) {
                return Response.fail("未选中库房或类型");
            }
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(warehouseId);
            if (null == group) {
                return Response.fail("库房或类型不存在");
            }
            ToolAccountStatisticsMapper mapper = factory.getMapper(ToolAccountStatisticsMapper.class);
            // 获取统计数据

            ToolStatisticsQuery query = new ToolStatisticsQuery();
            query.setWarehouseId(warehouseId);

            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper,
                    Arrays.asList(group), result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            ToolStatisticsBean statistics = mapper.getStatistics(query);
            // 获取工器具在库数
            Integer inWarehouseCount = mapper.getInWarehouseCount(query);
            statistics.setInWarehouseCount(inWarehouseCount);
            // 获取待检验工具数量
            Integer toInspectionCount = mapper.getToInspectionCount(query);
            statistics.setToInspectionCount(toInspectionCount);
            List<ToolStatisticsBean>list1 = new ArrayList<>();
            list1.add(statistics);
            return Response.ok(list1);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("getTotalList")
    public Response getTotalList(SessionFactory factory, ToolStatisticsQuery query) {
        try {
            if (StringUtils.isEmpty(query.getWarehouseId())) {
                return Response.fail("未选中库房或类型");
            }
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());
            if (null == group) {
                return Response.fail("库房或类型不存在");
            }
            ToolAccountStatisticsMapper mapper = factory.getMapper(ToolAccountStatisticsMapper.class);
            // 获取所有子集数据
            List<ToolGroupBean> childList = managerMapper.getChildList(group.getId());
            // 获取组织下的分类信息
            List<ToolGroupBean> list = childList.stream().filter(bean -> null != bean.getLevel() && bean.getLevel() > 3).collect(Collectors.toList());
            if (group.getLevel() > 3) {
                list.add(group);
            }
            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper, list, result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            PageHelper.startPage(query.getPage(), query.getPageSize());
            List<ToolBean> list1 = mapper.getTotalList(query);
            PageInfo pageInfo = new PageInfo(list1);
            return Response.ok(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("getInWarehouseList")
    public Response getInWarehouseList(SessionFactory factory, ToolStatisticsQuery query) {
        try {
            if (StringUtils.isEmpty(query.getWarehouseId())) {
                return Response.fail("未选中库房或类型");
            }
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());
            if (null == group) {
                return Response.fail("库房或类型不存在");
            }
            ToolAccountStatisticsMapper mapper = factory.getMapper(ToolAccountStatisticsMapper.class);
            // 获取所有子集数据
            List<ToolGroupBean> childList = managerMapper.getChildList(group.getId());
            // 获取组织下的分类信息
            List<ToolGroupBean> list = childList.stream().filter(bean -> null != bean.getLevel() && bean.getLevel() > 3).collect(Collectors.toList());
            if (group.getLevel() > 3) {
                list.add(group);
            }
            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper, list, result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            PageHelper.startPage(query.getPage(), query.getPageSize());
            List<ToolBean> list1 = mapper.getInWarehouseList(query);
            PageInfo pageInfo = new PageInfo(list1);
            return Response.ok(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("getOutWarehouseList")
    public Response getOutWarehouseList(SessionFactory factory, ToolStatisticsQuery query) {
        try {
            if (StringUtils.isEmpty(query.getWarehouseId())) {
                return Response.fail("未选中库房或类型");
            }
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());
            if (null == group) {
                return Response.fail("库房或类型不存在");
            }
            ToolAccountStatisticsMapper mapper = factory.getMapper(ToolAccountStatisticsMapper.class);
            // 获取所有子集数据
            List<ToolGroupBean> childList = managerMapper.getChildList(group.getId());
            // 获取组织下的分类信息
            List<ToolGroupBean> list = childList.stream().filter(bean -> null != bean.getLevel() && bean.getLevel() > 3).collect(Collectors.toList());
            if (group.getLevel() > 3) {
                list.add(group);
            }
            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper, list, result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            PageHelper.startPage(query.getPage(), query.getPageSize());
            List<ToolBean> list1 = mapper.getOutWarehouseList(query);
            PageInfo pageInfo = new PageInfo(list1);
            return Response.ok(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("getToInspectionList")
    public Response getToInspectionList(SessionFactory factory, ToolStatisticsQuery query) {
        try {
            if (StringUtils.isEmpty(query.getWarehouseId())) {
                return Response.fail("未选中库房或类型");
            }
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());
            if (null == group) {
                return Response.fail("库房或类型不存在");
            }
            ToolAccountStatisticsMapper mapper = factory.getMapper(ToolAccountStatisticsMapper.class);
            // 获取所有子集数据
            List<ToolGroupBean> childList = managerMapper.getChildList(group.getId());
            // 获取组织下的分类信息
            List<ToolGroupBean> list = childList.stream().filter(bean -> null != bean.getLevel() && bean.getLevel() > 3).collect(Collectors.toList());
            if (group.getLevel() > 3) {
                list.add(group);
            }
            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper, list, result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            // 获取下月时间
//            Calendar calendar = Calendar.getInstance();
//            calendar.add(Calendar.MONTH, 1);
//            long expireDate = calendar.getTimeInMillis()/1000L;
//            query.setExpireDate(expireDate);
            PageHelper.startPage(query.getPage(), query.getPageSize());
            List<ToolBean> list1 = mapper.getToInspectionList(query);
            PageInfo pageInfo = new PageInfo(list1);
            return Response.ok(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }

    @Override
    @TaskAnnotation("getScrapList")
    public Response getScrapList(SessionFactory factory, ToolStatisticsQuery query) {
        try {
            if (StringUtils.isEmpty(query.getWarehouseId())) {
                return Response.fail("未选中库房或类型");
            }
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());
            if (null == group) {
                return Response.fail("库房或类型不存在");
            }
            ToolAccountStatisticsMapper mapper = factory.getMapper(ToolAccountStatisticsMapper.class);
            // 获取所有子集数据
            List<ToolGroupBean> childList = managerMapper.getChildList(group.getId());
            // 获取组织下的分类信息
            List<ToolGroupBean> list = childList.stream().filter(bean -> null != bean.getLevel() && bean.getLevel() > 3).collect(Collectors.toList());
            if (group.getLevel() > 3) {
                list.add(group);
            }
            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper, list, result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            PageHelper.startPage(query.getPage(), query.getPageSize());
            List<ToolBean> list1 = mapper.getScrapList(query);
            PageInfo pageInfo = new PageInfo(list1);
            return Response.ok(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail();
        }
    }
}
